package com.tarena.lbs.business.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class BusinessRepositoryImpl implements BusinessRepository {
    @Autowired
    private BusinessMapper businessMapper;
    @Override
    public PageInfo<BusinessPO> SelectPageList(BusinessQuery query) {
        log.info("查询商家列表，入参:{}",query);
        QueryWrapper<BusinessPO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(query.getBusinessName())){
            queryWrapper.like("business_name",query.getBusinessName());
        }
        if (StringUtils.isNotBlank(query.getBusinessHeadPhone())){
            queryWrapper.eq("business_head_phone",query.getBusinessHeadPhone());
        }
        if (query.getBusinessStatus() != null){
            queryWrapper.eq("business_status",query.getBusinessStatus());
        }
        if (query.getStartingTime() != null){
            queryWrapper.ge("entry_time",query.getStartingTime());
        }
        PageHelper.startPage(query.getPageNo(),query.getPageSize());
        List<BusinessPO> pos = businessMapper.selectList(queryWrapper);
        return new PageInfo<>(pos);

    }

    @Override
    public void save(BusinessPO po) {
        log.info("保存商家信息，入参:{}",po);
        businessMapper.insert(po);
    }

    @Override
    public BusinessPO findById(Integer businessId) {
        log.info("查询商家信息，入参:{}",businessId);
        return businessMapper.selectById(businessId);
    }

    @Override
    public void updateStatusById(BusinessPO po) {
        log.info("更新商家状态，入参:{}",po);
        businessMapper.updateById(po);
    }
}
